<?php
namespace Home\Controller;
use Common\Controller\BaseController;
class WeixinController extends BaseController {
	//初始化方法
	protected function _initialize(){
		parent::_initialize();
	}
	
	//微信配置
    public function index(){
		$user=M('User');
		$where=array('username'=>$_SESSION['username']);
		
		if($_POST){
			$openid=I('post.openid');
			$appid=I('post.appid');
			$appsecret=I('post.appsecret');
			$token=I('post.token');
			
			$data=array('openid'=>$openid,'appid'=>$appid,'appsecret'=>$appsecret,'token'=>$token,);
			$rs=$user->where($where)->data($data)->save();
			if($rs){
				$this->success('配置成功');
			}else{
				$this->error('配置失败');
			}
			exit;
		}
		
		$result=$user->where($where)->find();
		$this->assign('openid',$result['openid']);
		$this->assign('appid',$result['appid']);
		$this->assign('appsecret',$result['appsecret']);
		$this->assign('token',$result['token']);
		
		//配置页面
		$this->display();	
    }
	
	//自定义菜单
	public function setmenu(){
		$user=M('User');
		$where=array('username'=>$_SESSION['username']);
		if($_POST){
			$menujson=I('post.menujson');
			$data=array('menujson'=>$menujson);
			$rs=$user->where($where)->data($data)->save();
			if($rs){
				$errormsg='已存储';
			}else{
				$errormsg='未存储';
			}
			
			$rs=$user->where($where)->field('accesstoken')->find();
			$access_token=$rs['accesstoken'];
			$url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}";
			$menujson=htmlspecialchars_decode($menujson);
			
			$ch=curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//数据流模式
			curl_setopt($ch, CURLOPT_POST, 1);//模拟POST
			curl_setopt($ch, CURLOPT_POSTFIELDS, $menujson);//post数据
			curl_setopt($ch, CURLOPT_TIMEOUT, 30);
			$result=curl_exec($ch);
			echo(curl_error($ch));
			curl_close($ch);
			
			$jsonarr=json_decode ($result,1);// 解析json
			if(0==$jsonarr['errcode']){
				$this->success('设置成功 '.$errormsg);
			}else{
				$this->error('设置失败 '.$errormsg);
			}
			exit;
		}
		
		$rs=$user->where($where)->field('tokenkey,menujson')->find();
		$menujson=htmlspecialchars_decode($rs['menujson']);
		if(empty($menujson)){
			$menujson='{
	"button":[{
		"type":"view",
		"name":"微官网",
		"url":"http://zzj5150.imwork.net/index.php/weiserver/weinet/index/tokenkey/%s"
	},
	{
		"name":"菜单",
		"sub_button":[{	
			"type":"view",
			"name":"搜索",
			"url":"https://m.baidu.com/"
		},
		{
			"type":"view",
			"name":"视频",
			"url":"http://v.qq.com/"
		},
		{
			"type":"click",
			"name":"赞一下我们",
			"key":"V1001_GOOD"
		}]
	}]
}';
			$menujson=sprintf($menujson,$rs['tokenkey']);
		}
		$this->assign('menujson',$menujson);
		$this->display();
	}
	
	//微官网
	public function weinet(){
		$weinet=M('Weinet');
		$tokenkey=$this->gettokenkey();
		$where=array('tokenkey'=>$tokenkey);
		$count=$weinet->where($where)->count();//查询满足要求的总记录数
		$Page=new \Think\Page($count,25);//实例化分页类 传入总记录数和每页显示的记录数(25)
		$show=$Page->show();//分页显示输出
		$list = $weinet->where($where)->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$this->assign('list',$list);//赋值数据集
		$this->assign('page',$show);//赋值分页输出
		
		$this->display();
	}
	
	//添加微官网首页列表单元
	public function addweinet(){
		if($_POST){
			$weinet=M('Weinet');
			$user=M('User');
			$username=$_SESSION['username'];
			$where=array('username'=>$username);
			
			$result=$user->where($where)->field('tokenkey')->find();
			$tokenkey=$result['tokenkey'];
			$title=I('post.title');
			$keyword=I('post.keyword');
			$img=I('post.img');
			
			$data=array(
				'tokenkey'=>$tokenkey,
				'title'=>$title,
				'keyword'=>$keyword,
				'img'=>$img,
				'time'=>time(),
			);
			$rs=$weinet->add($data);
			if($rs){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
			exit;
		}
		$this->display();
	}
	
	//编辑微官网首页列表单元
	public function editweinet(){
		$weinet=M('Weinet');
		$tokenkey=$this->gettokenkey();
		$id=I('get.id');
		$where=array('id'=>$id,'tokenkey'=>$tokenkey);
		
		if($_POST){
			$title=I('post.title');
			$keyword=I('post.keyword');
			$img=I('post.img');
			$secondjson=I('post.secondjson');
			
			$data=array(
				'title'=>$title,
				'keyword'=>$keyword,
				'img'=>$img,
				'secondjson'=>$secondjson,
				'time'=>time(),
			);
			
			$rs=$weinet->where($where)->save($data);
			if($rs){
				$this->success('编辑成功');
			}else{
				$this->error('编辑失败');
			}
			exit;
		}
		$rs=$weinet->where($where)->find();
		$secondjson=$rs['secondjson'];
		if(empty($secondjson)){
			$secondjson='{
	"type":"article",
	"title":"json格式说明",
	"content":[{
		"title":"<span style=\'background:#79F;color:#FFF;\'>页面json数据格式说明</span>",
		"content":[{
			"title":"文章页面json参数说明",
			"content":"
			<style type=\'text/css\'>
				table{width:100%;border:1px solid #CCC;}
				td{border:1px solid #CCC;}
			</style>
			<table cellspacing=\'0\'>
				<tr><td>字段</td><td>参数</td><td>说明</td></tr>
				<tr><td>type</td><td>article</td><td>指定为article页面类型</td></tr>
				<tr><td>title</td><td>字符串</td><td>设置文章题目</td></tr>
				<tr><td>content</td><td>json数据</td><td>一级content需要二级嵌套的json格式</td></tr>
			</table>"
		},
		{
			"title":"列表页面json参数说明",
			"content":"<table cellspacing=\'0\'>
				<tr><td>字段</td><td>参数</td><td>说明</td></tr>
				<tr><td>type</td><td>list</td><td>指定为list页面类型</td></tr>
				<tr><td>title</td><td>字符串</td><td>设置文章题目</td></tr>
				<tr><td>content</td><td>json数据</td><td>一级content需要二级嵌套的json格式</td></tr>
			</table>"
		}]
	},
	{
		"title":"章标题",
		"content":[{
			"title":"节标题",
			"content":"内容"
		},
		{
			"title":"节标题",
			"content":"内容"
		}]
	}]
}';
		}
		$secondjson=htmlspecialchars_decode($secondjson);
		$rs=$weinet->where($where)->find();
		$this->assign('title',$rs['title']);
		$this->assign('keyword',$rs['keyword']);
		$this->assign('img',$rs['img']);
		$this->assign('secondjson',$secondjson);
		$this->display();
	}
	
	//图文回复
	public function tuwen(){
		$tuwen=M('Tuwen');
		$tokenkey=$this->gettokenkey();
		$where=array('tokenkey'=>$tokenkey);
		$count=$tuwen->where($where)->count();//查询满足要求的总记录数
		$Page=new \Think\Page($count,25);//实例化分页类 传入总记录数和每页显示的记录数(25)
		$show=$Page->show();//分页显示输出
		$list=$tuwen->where($where)->order('time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$this->assign('list',$list);//赋值数据集
		$this->assign('page',$show);//赋值分页输出
		
		$this->display();
	}
	
	//添加图文回复
	public function addtuwen(){
		if($_POST){
			$tuwen=M('Tuwen');
			$tokenkey=$this->gettokenkey();
			$title=I('post.title');
			$keyword=I('post.keyword');
			$img=I('post.img');
			$content=I('post.content');
			
			$data=array(
				'tokenkey'=>$tokenkey,
				'title'=>$title,
				'keyword'=>$keyword,
				'img'=>$img,
				'content'=>$content,
				'time'=>time(),
			);
			$rs=$tuwen->add($data);
			if($rs){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
			exit;
		}
		$this->display();
	}
	
	//修改图文页面
	public function edittuwen(){
		$tuwen=M('Tuwen');
		$tokenkey=$this->gettokenkey();
		$id=I('get.id');
		$where=array('id'=>$id,'tokenkey'=>$tokenkey);
		if($_POST){
			$title=I('post.title');
			$keyword=I('post.keyword');
			$img=I('post.img');
			$content=I('post.content');
			
			$data=array(
				'title'=>$title,
				'keyword'=>$keyword,
				'img'=>$img,
				'content'=>$content,
				'time'=>time(),
			);
			$rs=$tuwen->where($where)->data($data)->save();
			if($rs){
				$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
			exit;
		}
		
		$result=$tuwen->where($where)->find();
		$this->assign('title',$result['title']);
		$this->assign('keyword',$result['keyword']);
		$this->assign('img',$result['img']);
		$this->assign('content',$result['content']);
		$this->display('addtuwen');
		//echo $tuwen->getLastSql();
	}
	
	//群发消息
	public function qunfa(){
		
		
		
		$this->display();
	}
	
	//系统客服
	public function localkefu(){
		
	}
	
	//系统客服
	public function officialkefu(){
		
	}
	
	//通过session[username]获取tokenkey
	private function gettokenkey(){
		$user=M('User');
		$username=$_SESSION['username'];
		$where=array('username'=>$username);
		$result=$user->where($where)->field('tokenkey')->find();
		$tokenkey=$result['tokenkey'];
		return $tokenkey;
	}
}